package cn.juque.redoctopus.bo.field;

import java.io.Serializable;
import lombok.Data;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.FutureArrays;

/**
 * @author nuoka
 * @version 1.0.0
 * <li></li>
 * @date 2021/11/13 18:20
 **/
@Data
public class BytesRefBO  implements Comparable<BytesRef>, Cloneable, Serializable {

    /** An empty byte array for convenience */
    public static final byte[] EMPTY_BYTES = new byte[0];

    /** The contents of the BytesRef. Should never be {@code null}. */
    private byte[] bytes;

    /** Offset of first valid byte. */
    private Integer offset;

    /** Length of used bytes. */
    private Integer length;

    /**
     * Creates and returns a copy of this object.  The precise meaning of "copy" may depend on the class of the object.
     * The general intent is that, for any object {@code x}, the expression:
     * <blockquote>
     * <pre>
     * x.clone() != x</pre></blockquote>
     * will be true, and that the expression:
     * <blockquote>
     * <pre>
     * x.clone().getClass() == x.getClass()</pre></blockquote>
     * will be {@code true}, but these are not absolute requirements. While it is typically the case that:
     * <blockquote>
     * <pre>
     * x.clone().equals(x)</pre></blockquote>
     * will be {@code true}, this is not an absolute requirement.
     * <p>
     * By convention, the returned object should be obtained by calling {@code super.clone}.  If a class and all of its
     * superclasses (except {@code Object}) obey this convention, it will be the case that {@code x.clone().getClass()
     * == x.getClass()}.
     * <p>
     * By convention, the object returned by this method should be independent of this object (which is being cloned).
     * To achieve this independence, it may be necessary to modify one or more fields of the object returned by {@code
     * super.clone} before returning it.  Typically, this means copying any mutable objects that comprise the internal
     * "deep structure" of the object being cloned and replacing the references to these objects with references to the
     * copies.  If a class contains only primitive fields or references to immutable objects, then it is usually the
     * case that no fields in the object returned by {@code super.clone} need to be modified.
     * <p>
     * The method {@code clone} for class {@code Object} performs a specific cloning operation. First, if the class of
     * this object does not implement the interface {@code Cloneable}, then a {@code CloneNotSupportedException} is
     * thrown. Note that all arrays are considered to implement the interface {@code Cloneable} and that the return type
     * of the {@code clone} method of an array type {@code T[]} is {@code T[]} where T is any reference or primitive
     * type. Otherwise, this method creates a new instance of the class of this object and initializes all its fields
     * with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the
     * fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy"
     * operation.
     * <p>
     * The class {@code Object} does not itself implement the interface {@code Cloneable}, so calling the {@code clone}
     * method on an object whose class is {@code Object} will result in throwing an exception at run time.
     *
     * @return a clone of this instance.
     * @throws CloneNotSupportedException if the object's class does not support the {@code Cloneable} interface.
     *                                    Subclasses that override the {@code clone} method can also throw this
     *                                    exception to indicate that an instance cannot be cloned.
     * @see Cloneable
     */
    @Override
    protected Object clone() throws CloneNotSupportedException {
        BytesRefBO bo = new BytesRefBO();
        bo.setBytes(this.bytes);
        bo.setOffset(this.offset);
        bo.setLength(this.length);
        return bo;
    }

    /**
     * Compares this object with the specified object for order.  Returns a negative integer, zero, or a positive
     * integer as this object is less than, equal to, or greater than the specified object.
     *
     * <p>The implementor must ensure <tt>sgn(x.compareTo(y)) ==
     * -sgn(y.compareTo(x))</tt> for all <tt>x</tt> and <tt>y</tt>.  (This implies that <tt>x.compareTo(y)</tt> must
     * throw an exception iff
     * <tt>y.compareTo(x)</tt> throws an exception.)
     *
     * <p>The implementor must also ensure that the relation is transitive:
     * <tt>(x.compareTo(y)&gt;0 &amp;&amp; y.compareTo(z)&gt;0)</tt> implies
     * <tt>x.compareTo(z)&gt;0</tt>.
     *
     * <p>Finally, the implementor must ensure that <tt>x.compareTo(y)==0</tt>
     * implies that <tt>sgn(x.compareTo(z)) == sgn(y.compareTo(z))</tt>, for all <tt>z</tt>.
     *
     * <p>It is strongly recommended, but <i>not</i> strictly required that
     * <tt>(x.compareTo(y)==0) == (x.equals(y))</tt>.  Generally speaking, any
     * class that implements the <tt>Comparable</tt> interface and violates this condition should clearly indicate this
     * fact.  The recommended language is "Note: this class has a natural ordering that is inconsistent with equals."
     *
     * <p>In the foregoing description, the notation
     * <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical
     * <i>signum</i> function, which is defined to return one of <tt>-1</tt>,
     * <tt>0</tt>, or <tt>1</tt> according to whether the value of
     * <i>expression</i> is negative, zero or positive.
     *
     * @param other the object to be compared.
     * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than
     * the specified object.
     * @throws NullPointerException if the specified object is null
     * @throws ClassCastException   if the specified object's type prevents it from being compared to this object.
     */
    @Override
    public int compareTo(BytesRef other) {
        return FutureArrays.compareUnsigned(this.bytes, this.offset, this.offset + this.length,
            other.bytes, other.offset, other.offset + other.length);
    }
}
